package campus.aiit.ssl.domain.model;

import campus.aiit.ssl.manage.modbus.provider.Provider;
import lombok.Getter;
import lombok.Setter;

import java.sql.Time;
import java.time.LocalTime;
import java.util.Date;

@Getter@Setter
public class Week {
  private Long id;

  private Long strategyId;

  private Byte day;

  private Time startTime;

  private Time endTime;

  private Integer summerLowestT;

  private Integer summerHighestT;

  private Integer summerConstantT;

  private Integer winterLowestT;

  private Integer winterHighestT;

  private Integer winterConstantT;

  private String content;

  private Date createTime;

  private Date updateTime;

  public String toHexString(){
    StringBuilder builder = new StringBuilder();
    switch (this.getDay()){
      case 1:
        builder.append(Provider.Constants.PDUs.MONDAY.getHex());
        break;
      case 2:
        builder.append(Provider.Constants.PDUs.TUESDAY.getHex());
        break;
      case 3:
        builder.append(Provider.Constants.PDUs.WEDNESDAY.getHex());
        break;
      case 4:
        builder.append(Provider.Constants.PDUs.THURSDAY.getHex());
        break;
      case 5:
        builder.append(Provider.Constants.PDUs.FRIDAY.getHex());
        break;
      case 6:
        builder.append(Provider.Constants.PDUs.SATURDAY.getHex());
        break;
      case 7:
        builder.append(Provider.Constants.PDUs.SUNDAY.getHex());
        break;
      default:
        System.err.println("Fatal Error: day field not in [1, 7]");
        break;
    }
    // start time
    int startHour = this.getStartTime().toLocalTime().getHour();
    int startMinute = this.getStartTime().toLocalTime().getMinute();
    builder.append(time2HexString(startHour * 60 + startMinute));
    // end time
    int endHour = this.getEndTime().toLocalTime().getHour();
    int endMinute = this.getEndTime().toLocalTime().getMinute();
    builder.append(time2HexString(endHour * 60 + endMinute));
    // summer highest temperature
    builder.append(temperature2HexString(this.getSummerHighestT()));
    // summer lowest temperature
    builder.append(temperature2HexString(this.getSummerLowestT()));
    // winter lowest temperature
    builder.append(temperature2HexString(this.getWinterLowestT()));
    // winter highest temperature
    builder.append(temperature2HexString(this.getWinterHighestT()));
    // summer constant temperature
    builder.append(temperature2HexString(this.getSummerConstantT()));
    // winter constant temperature
    builder.append(temperature2HexString(this.getWinterConstantT()));
    // reserve bytes
    builder.append(Provider.Constants.PDUs.RESERVE_BYTES.getHex());
    // 人体红外+设备控制模式
    builder.append(Provider.Constants.PDUs.HUMAN_INFRARED.getHex());
    builder.append(Provider.Constants.PDUs.DEVICE_CONTROL_MODE.getHex());
    return builder.toString().replaceAll(" ", ""); // trim space finally
  }
  private String time2HexString(int time){
    StringBuilder builder = new StringBuilder(Integer.toHexString(time));
    // fill 0 if short of length
    while (builder.length() < 4){
      builder.insert(0, 0);
    }
    return builder.toString();
  }
  private String temperature2HexString(int temperature){
    StringBuilder builder = new StringBuilder(Integer.toHexString(temperature));
    while (builder.length() < 2){
      builder.insert(0, 0);
    }
    return builder.toString();
  }

  public static void main(String[] args) {
    Week week = new Week();
    week.setDay((byte) 1);
    week.setStartTime(Time.valueOf(LocalTime.of(0, 0)));
    week.setEndTime(Time.valueOf(LocalTime.of(7, 59)));
    week.setSummerHighestT(33);
    week.setSummerLowestT(30);
    week.setWinterLowestT(30);
    week.setWinterHighestT(33);
    week.setSummerConstantT(26);
    week.setWinterConstantT(18);
    System.out.println(week.toHexString());
  }
}